<?php

defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
 
$tpl=new TemplatePower("skin/sk_menu.htm");
$tpl->prepare();
$id=intval($_GET['id']);
$pid=intval($_REQUEST['pid']);
$par_page="sr_menu";
$id_category="id_menu";
$category="menu".$lang;
$tpl->assignGlobal("lang",$lang);
$type_menu=compile_post("type_menu");
$tpl->assignGlobal("title","Quản lý menus");
$tpl->assignGlobal("par_page",$par_page);

$ct=new class_tree($pid);
$ct->get_cat_tree(0,$category,$id_category);	
$ct->get_cat_string_admin($pid,$par_page,$category,$id_category);
$navigator.=$catstring2;								
$tpl->assignGlobal("pathpage","<a href='?page=sr_menu'>Danh sách menus</a>");
if(!$_GET['code'] or $_GET['code']=='00'){
	show_list();
}
if($_GET['code']=='01'){
	$tpl->newBlock("addnewitem");
	$info=array();
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$tpl->assign("thu_tu",auto_order($category,'thu_tu',$pid));
	show_list();
}
if($_GET['code']=='02'){
	
	$name=compile_post("name");
	if($name!=''){
		$a=array();
		$a['name']		=$name;
		$a['active']	=intval(compile_post("active"));
		$a['thu_tu']	=intval(compile_post("thu_tu"));
		$a['inhome']	=intval(compile_post("inhome"));
		$a['link_write']=compile_post("link");		
		$a['idcategory']=intval(compile_post("idcategory"));
		$a['type_menu']	=compile_post("type_menu");
		$a['parentid']	=intval(compile_post('parentid'));
		$vitrimenu=$_POST['vitri'];
		$vitri='0';
		if($vitrimenu){
			foreach($vitrimenu as $vt){
				$vitri.=",".$vt;	
			}
		}
		$a['vitri']=$vitri;
		if($_FILES['image']['size']){
			$image=uploadimage('image')	;
			$a['image']=$image['image'];
			$a['normal_image']=$image['normal_image'];
			$a['small_image']=$image['small_image'];
		}
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO $category (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		$id_menu_new=mysql_insert_id();
		
		update_id_menu(compile_post("type_menu"),intval(compile_post("idcategory")));
		
		$sql="SELECT * FROM $category WHERE parentid=".intval(compile_post('parentid'))." ORDER BY thu_tu ASC";
		$db=$DB->query($sql);
		$i=0;
		while($rs=mysql_fetch_array($db)){
			$i++;
			$DB->query("UPDATE $category SET thu_tu=$i WHERE id_menu=$rs[id_menu]");	
		}
		message("Th&#234;m m&#7899;i menu th&#224;nh c&#244;ng !");
	}else{
		message("Không đủ dữ liệu đầu vào !");
	}
	
	show_list();

}
if($_GET['code']=='03'){
	$tpl->newBlock("edititem");
	$info=array();
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$sql="SELECT * FROM $category WHERE id_menu=$id";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",		$rs['name']);
		$tpl->assign("link",		$rs['link_write']);
		$tpl->assign("idcategory",		$rs['idcategory']);
		$tpl->assign("thu_tu",		$rs['thu_tu']);
		$tpl->assign("id_menu",		$rs['id_menu']);
		if($rs['small_image'])
		{
			$tpl->assign('image',"<img src='".'../'.$CONFIG['upload_image_path'].$rs['small_image']."' ><br><input type='checkbox' name='xoa_anh' value='1' class='noborder'>&nbsp;X&#243;a &#7843;nh<br>");		
		}
		if($rs['active']=='1') $tpl->assign("active","checked");
		
	
		
		$tpl->assign($rs["type_menu"],"selected='selected'");
		$vitri=explode(",",$rs['vitri']);
		foreach($vitri as $selectvt){
			$tpl->assign($selectvt,"checked");
		}
	}
	show_list();
}
if($_GET['code']=='04'){
	$a=array();
	$name=compile_post("name");
	
	
	if($name!=''){
		$a=array();
		$a['name']		=$name;
		$a['active']	=intval(compile_post("active"));
		$a['inhome']	=intval(compile_post("inhome"));
		$a['thu_tu']	=intval(compile_post("thu_tu"));
		$a['link']		=compile_post("link");
		$a['type_menu']	=compile_post("type_menu");
		
			$a['idcategory']=intval(compile_post("idcategory"));
		
		$a['parentid']	=intval(compile_post('parentid'));
		$vitrimenu=$_POST['vitri'];
		$vitri='0';
		if($vitrimenu){
			foreach($vitrimenu as $vt){
				$vitri.=",".$vt;	
			}
		}
		$a['vitri']=$vitri;
		if($_FILES['image']['size'] or compile_post('xoa_anh')==1){
			$sql="SELECT * FROM $category WHERE id_menu=$id";
			$db=$DB->query($sql);
			if($rs=mysql_fetch_array($db)){
				deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);
				$a['image']='';
				$a['normal_image']='';
				$a['small_image']='';	
			}
			if($_FILES['image']['size']){
				$image=uploadimage('image')	;
				$a['image']=$image['image'];
				$a['normal_image']=$image['normal_image'];
				$a['small_image']=$image['small_image'];
			}
		}
		
		$sql="SELECT * FROM $category WHERE id_menu=$id";
		$db=$DB->query($sql);
			if($rs=mysql_fetch_array($db)){
				$id_menu_new=$id;
				update_id_menu_delete($rs['id_menu'],$rs['type_menu']);
				update_id_menu(compile_post("type_menu"),intval(compile_post("idcategory")));
			}
		
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE $category SET ".$b." WHERE id_menu=".$id;
		$DB->query($sql);
		
		
		$sql="SELECT * FROM $category WHERE parentid=".intval(compile_post('parentid'))." ORDER BY thu_tu ASC";
		$db=$DB->query($sql);
		$i=0;
		while($rs=mysql_fetch_array($db)){
			$i++;
			$DB->query("UPDATE $category SET thu_tu=$i WHERE id_menu=$rs[id_menu]");	
		}
		message("S&#7917;a th&#224;nh c&#244;ng !");
	}else{
		message("Không đủ dữ liệu đầu vào !");
	}
	
	show_list();
}

if($_GET['code']=='05'){
	if(check_menu_in($id)){
		$sql="SELECT * FROM $category WHERE id_menu=$id";
		$db=$DB->query($sql);
		if($rs=mysql_fetch_array($db)){
			update_id_menu_delete($rs['id_menu'],$rs['type_menu']);
		}
		$sql="DELETE FROM $category WHERE id_menu=$id";
		$db=$DB->query($sql);
		if($db) message("Đã xóa xong !");
		else message("Lỗi không xóa được !");
	}
	
	show_list();

}

if($_GET['code']=='07'){
	
	$sql="DELETE FROM $category WHERE id_menu IN(".Get_All_Idmenusub($id).")";
	$db=$DB->query($sql);
	if($db) message("Đã xóa xong !");
	else message("Lỗi không xóa được !");
	show_list();
	
}
function Get_All_Idmenusub($id){
	global $DB,$category;
	$list_id=intval($id);
	$sql="SELECT * FROM $category WHERE parentid=$id";
	$db=$DB->query($sql);
	while($rs=mysql_fetch_array($db)){
		$list_id.=",".Get_All_Idmenusub($rs['id_menu']);
	}
	return $list_id;
}

if($_GET['code']=='06'){
		$sql="SELECT * FROM menu ORDER BY thu_tu DESC, name ASC";
		$c=$DB->query($sql);
		$a=array();
		while ($d=mysql_fetch_array($c))
		{
			$active=compile_post('active_'.$d['id_menu']);
			if ($active!=$d['active'])
			{
				$a['active']=intval($active);
				$b=$DB->compile_db_update_string($a);
				$sql="UPDATE menu SET ".$b." WHERE id_menu=".$d['id_menu'];
				$ok=$DB->query($sql);
			}
			$thu_tu=compile_post('thu_tu_'.$d['id_menu']);
			if ($thu_tu!=$d['thu_tu'])
			{
				$a['thu_tu']=intval($thu_tu);
				$b=$DB->compile_db_update_string($a);
				$sql="UPDATE menu SET ".$b." WHERE id_menu=".$d['id_menu'];
			
				$ok=$DB->query($sql);
			}
		}
		if($ok) message("L&#432;u th&#224;nh c&#244;ng !");
	
	show_list();
}
function check_menu_in($id){
	global $DB,$category;
	$sql="SELECT * FROM $category WHERE parentid=$id";
	$db=$DB->query($sql);
	if(mysql_num_rows($db)>0){
		message("Menu này có chứa các menu cấp trong, bạn có muốn xóa tất cả các menu trong không? <a href='?page=sr_menu&code=07&id=$id'>Có</a>"	);
		return false;
	}else{
		return true;	
	}
}

function show_list($pid=0){
	global $DB, $tpl,$category;
	$pid=intval($pid);
	//$tpl->newBlock("cell_list");
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu ASC, name ASC";
	$db=$DB->query($sql);
	while($rs=mysql_fetch_array($db)){
		$tpl->newBlock("list_menu");
		$tpl->assign("name",		"├&nbsp;<strong>".$rs['name']."</strong>");
		$tpl->assign("link",		$rs['link']);
		$tpl->assign("thu_tu",		$rs['thu_tu']);
		if($rs['active']==1) $tpl->assign("active","checked");
		$tpl->assign("id_menu",		$rs['id_menu']);
		$tpl->assign("parentid",		$rs['parentid']);
		//$tpl->assign("type_menu",	$rs['type_menu']);
		$tpl->assign("vitri",substr($rs['vitri'],2));	
		if($rs['type_menu']=='')	$tpl->assign("type_menu","Not set");
		else $tpl->assign("type_menu",	$rs['type_menu']);
		show_list1($rs['id_menu']);
	}
}
function show_list1($pid=0){
	global $DB, $tpl,$category;
	$pid=intval($pid);
	//$tpl->newBlock("cell_list");
	
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu ASC, name ASC";
	$db=$DB->query($sql);
	if(mysql_num_rows($db)>0)
	while($rs=mysql_fetch_array($db)){
		$tpl->newBlock("list_menu");
		$tpl->assign("name",		"&nbsp;&nbsp;&nbsp;&nbsp;└--&nbsp;".$rs['name']);
		$tpl->assign("link",		$rs['link']);
		$tpl->assign("thu_tu",		$rs['thu_tu']);
		if($rs['active']==1) $tpl->assign("active","checked");
		$tpl->assign("id_menu",		$rs['id_menu']);
		$tpl->assign("parentid",		$rs['parentid']);
		$tpl->assign("type_menu",	$rs['type_menu']);
			
		if($rs['type_menu']=='')	$tpl->assign("type_menu","Not set");
		else $tpl->assign("type_menu",	$rs['type_menu']);
		show_list2($rs['id_menu']);
	}
}
function show_list2($pid=0){
	global $DB, $tpl,$category;
	$pid=intval($pid);
	//$tpl->newBlock("cell_list");
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu ASC, name ASC";
	$db=$DB->query($sql);
	if(mysql_num_rows($db)>0)
	while($rs=mysql_fetch_array($db)){
		$tpl->newBlock("list_menu");
		$tpl->assign("name",		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└--&nbsp;<em>".$rs['name']."</em>");
		$tpl->assign("link",		$rs['link']);
		$tpl->assign("thu_tu",		$rs['thu_tu']);
		if($rs['active']==1) $tpl->assign("active","checked");
		$tpl->assign("id_menu",		$rs['id_menu']);
		$tpl->assign("parentid",		$rs['parentid']);
		//$tpl->assign("type_menu",	$rs['type_menu']);
			
		if($rs['type_menu']=='')	$tpl->assign("type_menu","Not set");
		else $tpl->assign("type_menu",	$rs['type_menu']);
		show_list3($rs['id_menu']);
	}
}
function show_list3($pid=0){
	global $DB, $tpl,$category;
	$pid=intval($pid);
	//$tpl->newBlock("cell_list");
	
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu ASC, name ASC";
	$db=$DB->query($sql);
	if(mysql_num_rows($db)>0)
	while($rs=mysql_fetch_array($db)){
		$tpl->newBlock("list_menu");
		$tpl->assign("name",		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└-----&nbsp;".$rs['name']);
		$tpl->assign("link",		$rs['link']);
		$tpl->assign("thu_tu",		$rs['thu_tu']);
		if($rs['active']==1) $tpl->assign("active","checked");
		$tpl->assign("id_menu",		$rs['id_menu']);
		$tpl->assign("parentid",		$rs['parentid']);
	
		if($rs['type_menu']=='')	$tpl->assign("type_menu","Not set");
		else $tpl->assign("type_menu",	$rs['type_menu']);
		
	}
}



function trimlinkwrite($str){
	$strs=array();
	$strs=explode('_',$str);
	return $strs[0];
}

function update_id_menu($ty_menu,$idcate){
	global $arrtable,$DB,$id_menu_new;
	if($ty_menu!=''){
		foreach($arrtable as $table=>$id_ca){
				switch($ty_menu){
					case 'news':
						$tb='cat';
						$id_ca='id_cat';
						break;
					case 'products':
						$tb='catpd';
						$id_ca='id_catpd';
						break;
					case 'catpt':
						$tb='catpt';
						$id_ca='id_catpt';
						break;
					case 'info':
						$tb='catif';
						$id_ca='id_catif';
						break;
				}
				if($tb){
					$sqlx='UPDATE '.$tb.' SET id_menu='.intval($id_menu_new).' WHERE '.$id_ca.'='.intval($idcate);
					$DB->query($sqlx);				
				}
		}
	}
}
function update_id_menu_delete($idmenu,$ty_menu){
	global $arrtable,$DB;
	if($ty_menu!=''){
		foreach($arrtable as $table=>$id_ca){
				switch($ty_menu){
					case 'news':
						$tb='cat';
						$id_ca='id_cat';
						break;
					case 'products':
						$tb='catpd';
						$id_ca='id_catpd';
						break;
					case 'catpt':
						$tb='catpt';
						$id_ca='id_catpt';
						break;
					case 'info':
						$tb='catif';
						$id_ca='id_catif';
						break;
				}
				if($tb){
					$sqlx='UPDATE '.$tb.' SET id_menu=0 WHERE id_menu='.intval($idmenu);
					$DB->query($sqlx);				
				}
		}
	}
}
$tpl->printToScreen();
?>